// components/details/details.js
const sharePost = require('../../request/sharePost.js')
Component({
  /**
   * 组件的属性列表
   */
  /**
   * 组件的属性列表
   */
  properties: {
    commentDetails: {
      type: Object,
      value: {},
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    salesman_remarks: '',
    submit_audit_status: '',
    items: [{
        name: '未上门',
        value: '1'
      },
      {
        name: '已上门',
        value: '2'
      },
      {
        name: '已成交',
        value: '3'
      },
      {
        name: '失效',
        value: '4'
      }
    ]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    closeDetail(e) {
      let isClose = 'none'
      this.triggerEvent('closeCommit', {
        isDetail: isClose,
        isUpdate: "false"
      });
    },

    salesmanRemarksInput: function(e) {
      //备注
      this.setData({
        salesman_remarks: e.detail.value
      })
    },
    //保存业务员备注
    updateRemarks(e){
      let salesmanRemarks = this.data.salesman_remarks; //是否修改备注
      //业务员备注
      if (salesmanRemarks == "") {
        //备注
        salesmanRemarks = this.data.commentDetails.salesman_remarks;
        if (typeof (salesmanRemarks) == "undefined") {
          salesmanRemarks = "";
        }

      } else {
        salesmanRemarks = this.data.salesman_remarks;
      }
      //参数
      let  params = {
        "orderId": this.data.commentDetails._id, //id
        "salesman_remarks": salesmanRemarks  //业务员备注 
      }
      console.log('查看params', params)
      sharePost.getUpdateOrder(params).then(res => {
        console.log('更新结果', res) 
        if (res.code == 0) {
          wx.showToast({
            title: '保存成功',
            icon: 'succes',
            duration: 1000,
            mask: true
          })
          let isClose = 'none'
          this.triggerEvent('closeCommit', {
            isDetail: isClose,
            isUpdate: "true"
          });
        } else {
          wx.showToast({
            title: '保存失败',
            icon: 'succes',
            duration: 1000,
            mask: true
          })
        }
      })
    },
    //保存服务状态
    updateExamine(e){
      let recommendedState = this.data.commentDetails.recommended_state; //默认服务状态
      let submitAuditStatus = this.data.submit_audit_status; //是否修改了服务状态
      if (submitAuditStatus == "") {
        submitAuditStatus = this.data.commentDetails.submit_audit_status;
        if (submitAuditStatus == "") {
          submitAuditStatus = recommendedState;
        } else if (typeof (submitAuditStatus) == "undefined") {
          submitAuditStatus = recommendedState;
        }
      } else {
        submitAuditStatus = this.data.submit_audit_status;
      }

      let params = {
        "orderId": this.data.commentDetails._id, //id 
        "audit_status": "2", //1.还未修改 2.待审核 3.审核通过
        "submit_audit_status": submitAuditStatus //1.未上门 2.已上门 3.已成交 4.失效
      }
      console.log('查看params', params)
      sharePost.getUpdateOrder(params).then(res => {
        console.log('更新结果', res)
        if (res.code == 0) {
          wx.showToast({
            title: '保存成功',
            icon: 'succes',
            duration: 1000,
            mask: true
          })
          let isClose = 'none'
          this.triggerEvent('closeCommit', {
            isDetail: isClose,
            isUpdate: "true"
          });
        } else {
          wx.showToast({
            title: '保存失败',
            icon: 'succes',
            duration: 1000,
            mask: true
          })
        }
      })
    },
    updateDetail(e) {
      let recommendedState = this.data.commentDetails.recommended_state; //默认服务状态
      let salesmanRemarks = this.data.salesman_remarks; //是否修改备注
      let submitAuditStatus = this.data.submit_audit_status; //是否修改了服务状态
      let auditStatus = this.data.commentDetails.audit_status; //审核状态
      

      //业务员备注
      if (salesmanRemarks == "") {
        //备注
        salesmanRemarks = this.data.commentDetails.salesman_remarks;
        if (typeof(salesmanRemarks) == "undefined") {
          salesmanRemarks = "";
        }

      } else {
        salesmanRemarks = this.data.salesman_remarks;
      }
     
      //提交审核状态
      if (submitAuditStatus == "") {
        submitAuditStatus = this.data.commentDetails.submit_audit_status;

        if (submitAuditStatus == "") {
          submitAuditStatus = recommendedState;
        } else if (typeof(submitAuditStatus) == "undefined") {
          submitAuditStatus = recommendedState;
        }
      } else {
        submitAuditStatus = this.data.submit_audit_status;
      }
      
      let params={};
      if (auditStatus==1){
        //提交审核 
        params = {
          "orderId": this.data.commentDetails._id, //id
          "salesman_remarks": salesmanRemarks, //业务员备注
          "audit_status": "2", //1.还未修改 2.待审核 3.审核通过
          "submit_audit_status": submitAuditStatus //1.未上门 2.已上门 3.已成交 4.失效
        }
      }else{
        //提交审核 
        params = {
          "orderId": this.data.commentDetails._id, //id
          "salesman_remarks": salesmanRemarks  //业务员备注 
        }
      }

      // //提交审核 
      // let params = {
      //   "orderId": this.data.commentDetails._id, //id
      //   "salesman_remarks": salesmanRemarks, //业务员备注
      //   "audit_status": 2, //1.还未修改 2.待审核 3.审核通过
      //   "submit_audit_status": submitAuditStatus //1.未上门 2.已上门 3.已成交 4.失效
      // }
      console.log('查看params', params)
      // sharePost.getUpdateOrder(params).then(res => {
      //   console.log('更新结果', res)
      //   console.log('更新结果', res.code)
      //   if (res.code == 0) {
      //     wx.showToast({
      //       title: '更新成功',
      //       icon: 'succes',
      //       duration: 1000,
      //       mask: true
      //     })
      //     let isClose = 'none'
      //     this.triggerEvent('closeCommit', {
      //       isDetail: isClose,
      //       isUpdate: "true"
      //     });
      //   } else {
      //     wx.showToast({
      //       title: '更新失败',
      //       icon: 'succes',
      //       duration: 1000,
      //       mask: true
      //     })
      //   }
      // })


      //let isClose = 'none'
      //this.triggerEvent('closeCommit', { isDetail: isClose, isUpdate:"true"});
    },
    radioChange: function(e) {
      console.log('radio发生change事件，携带value值为：', e.detail.value)
      //提交待审核服务状态
      this.setData({
        submit_audit_status: e.detail.value
      })
    }
  },
  /**
   * 组件挂载后执行
   */
  ready: function() {
    console.log('this.data.commentDetails', this.data.commentDetails)
  }
})